#include <stdio.h>
#include <stdlib.h>

void read_from_buffer(void);

void read_txt_file(void);

long get_long(void);

void echo_eof(void);

void echo_file(void);

void gress(void);

void show_char(void);

int main(void)
{
    show_char();
    return 0;
}

void read_from_buffer(void)
{
    int ch;

    // Windows10 CLion MinGW 使用CTRL+D输入EOF
    while ((ch = getchar()) != EOF)
    {
        putchar(ch);
    }
}

void read_txt_file(void)
{
    int ch;
    FILE *fp;
    char file_name[50];

    printf("请输入文件路径: ");
    fflush(stdout);
    scanf("%s", file_name);


    fp = fopen(file_name, "r");
    if (fp == NULL)
    {
        printf("文件不存在, path: [%s]\n", file_name);
        exit(1);
    }

    printf("file path: [%s], 文件内容如下\n", file_name);
    while ((ch = getc(fp)) != EOF)
    {
        putchar(ch);
    }

    fclose(fp);
}

long get_long(void)
{
    long input;
    int ch;

    while (scanf("%ld", &input) != 1)
    {
        while ((ch = getchar()) != '\n')
        {
            putchar(ch);
        }
//        putchar('\n');
    }

    return input;
}

void echo_eof()
{
    int ch;

    while ((ch = getchar()) != EOF)
    {
        putchar(ch);
    }
}

void echo_file()
{
    int ch;
    FILE *fp;
    char fname[50];

    printf("enter the name of the file: ");
    fflush(stdout);
    scanf("%s", fname);
    fp = fopen(fname, "r");

    if (fp == NULL)
    {
        printf("failed to open file.\n");
        return;
    }

    while ((ch = getc(fp)) != EOF)
    {
        putchar(ch);
    }
    fclose(fp);
}

void gress(void)
{
    int gress = 1;

    printf("pick an integer from 1-100. i will try to gress\n");

    printf("is it %d\n", gress++);
    while (getchar() != 'y')
    {
        printf("is it %d\n", gress++);

        // 2 因此 要在此将\n字符消耗掉
        while (getchar() != '\n')
        {
            continue;
        }
    }

    printf("i knew i cloud do it!\n");
}

void show_char(void)
{
    int ch;
    int rows, cols;

    printf("enter character and two integers.\n");

    while ((ch = getchar()) != '\n')
    {
        if (scanf("%d %d", &rows, &cols) != 2)
        {
            break;
        }

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; ++j)
            {
                putchar(ch);
            }
            putchar('\n');
        }

        // 清理后续的无效字符
        while (getchar() != '\n')
        {
            continue;
        }
        printf("enter new character and two integers.\n");
    }

    printf("bye\n");
}
